﻿//-----------------------------------------------------------------------------------------------------------
// Wbfs Engine (WBFSe) Project by Omega Frost 
// http://wbfse.codeplex.com/
//
// WBFSe is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;

namespace WBFSe.IO
{
    /// <summary>
    /// Delegate for a stream closing event
    /// </summary>
    /// <param name="sender">Closing Stream</param>
    public delegate void StreamClosingDelegate(IWbfsStream sender);

    /// <summary>
    /// Representation of a stream used by the engine
    /// </summary>
    public interface IWbfsStream
    {
        /// <summary>
        /// Indicates that the stream is closing
        /// </summary>
        event StreamClosingDelegate Closing;

        /// <summary>
        /// Retrieves the file pointer
        /// </summary>
        IntPtr FilePtr { get; }

        /// <summary>
        /// Retrieves the stream length
        /// </summary>
        /// <exception cref="System.IOException">On operation failure</exception>
        long Length { get; }

        /// <summary>
        /// Retrieves the physical size
        /// </summary>
        /// <exception cref="System.IOException">On operation failure</exception>
        long Size { get; }

        /// <summary>
        /// Reads data from the stream
        /// </summary>
        /// <param name="pMem">Unmanaged memory</param>
        /// <param name="offset">Offset from pMem</param>
        /// <param name="position">Destination position</param>
        /// <param name="count">Number of bytes to be read</param>
        /// <returns>Number of bytes read</returns>
        /// <exception cref="System.IOException">On operation failure</exception>
        uint Read(IntPtr pMem, int offset, long position, int count);

        /// <summary>
        /// Writes data from the stream
        /// </summary>
        /// <param name="pMem">Unmanaged memory</param>
        /// <param name="offset">Offset from pMem</param>
        /// <param name="position">Destination position</param>
        /// <param name="count">Number of bytes to be written</param>
        /// <returns>Number of bytes written</returns>
        /// <exception cref="System.IOException">On operation failure</exception>
        uint Write(IntPtr pMem, int offset, long position, int count);

        /// <summary>
        /// Sets a region as empty
        /// </summary>
        /// <param name="position">Destination position</param>
        /// <param name="count">Number of bytes to set</param>
        /// <returns>Number of bytes set</returns>
        /// <exception cref="System.IOException">On operation failure</exception>
        void WriteEmpty(long position, int count);

        /// <summary>
        /// Closes the stream, this method can fail silently
        /// </summary>
        void Close();
    }
}
